package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission select(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo(permissions);
    }

    @Override
    public void reload() {
        //查询全部权限表达式
        List<String> expressions = permissionMapper.selectAllExpression();
        //从spring容器中获取所有的controller
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //获取所有value值
        Collection<Object> values = beans.values();
        //遍历循环获取每一个controller
        for (Object controller : values) {
            //获取controller的字节码对象(获取该类的父类)
            Class clazz = controller.getClass().getSuperclass();
            //获取controller的所有方法
            Method[] methods = clazz.getDeclaredMethods();
            //遍历出每一个方法
            for (Method method : methods) {
                //判断是否有帖子定义的权限注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //如果有贴,封装成权限对象，并插入数据库
                if (annotation != null) {
                    //获取权限表达式
                    String expression = annotation.value()[0];
                    //判断权限表达式是否已经存在数据库中，如果不存在，就需要插入
                    if (!expressions.contains(expression)) {
                        Permission permission = new Permission();
                        permission.setName(annotation.value()[1]);//中文权限名称
                        permission.setExpression(expression);//英文权限表达式
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectByEmpId(id);
    }


}
